<?php


namespace App\Services\Result;

use App\Interf\Resultable;
use Monolog\Logger;

use ParamConfig;
use App\Models\LogHelper;

class FigureResult extends BaseResult implements Resultable
{
    private $answers;

    private $summaryObj;

    private $bodyStrengthParametersObj;

    private $dietaryAnalysisAndRecommendationsObj;

    private $physicalAnalysisAndSuggestionObj;



    public function __construct($answersStr = null)
    {

        $this->answers = $this->convertAnswer($answersStr);
        $this->summaryObj = $this->configAttrObj(1, '总体描述');
        $this->bodyStrengthParametersObj = $this->configAttrObj(2, '身材剖析');
        $this->dietaryAnalysisAndRecommendationsObj = $this->configAttrObj(3, '饮食建议');
        $this->physicalAnalysisAndSuggestionObj = $this->configAttrObj(4, '体质分析');


    }


    public function result()
    {

        $attrs = [];
        array_push($attrs, $this->getSummaryAttr());
        array_push($attrs, $this->getBodyStrengthParametersAttr());
        array_push($attrs, $this->getDietaryAnalysisAndRecommendationsAttr());
        array_push($attrs, $this->getPhysicalAnalysisAndSuggestionAttr());


        $data['attrs'] = $attrs;
        $data['chatparam'] = $this->getChatparam();
        //图类型常规图
        $data['chatType'] = $this->getChatType();
        return $data;
    }


    protected function getSummaryAttr()
    {
        $note = '';
        $extra = '';
        $answer8 = $this->getDefaultAnswerObjByIndex(8);
        $answer9 = $this->getDefaultAnswerObjByIndex(9);
        $answer19 = $this->getDefaultAnswerObjByIndex(19);
        $answer26 = $this->getDefaultAnswerObjByIndex(26);
        $answer1 = $this->getDefaultAnswerObjByIndex(1);
        $answer2 = $this->getDefaultAnswerObjByIndex(2);

        if ($answer26['weight'] <= 0) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.one.one');
            $note .= $temp;
        } elseif ($answer26['weight'] > 0 && $answer26['weight'] <= 10) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.one.two');
            $note .= $temp;
        } elseif ($answer26['weight'] > 10 && $answer26['weight'] <= 20) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.one.three');
            $note .= $temp;
        } elseif ($answer26['weight'] > 20 && $answer26['weight'] <= 32) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.one.four');
            $note .= $temp;
        }

        if ($answer1['weight'] == 2 && $answer2['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.two.one');
            $note .= $temp;
        } elseif ($answer1['weight'] == 2 && $answer2['weight'] == 2) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.two.two');
            $note .= $temp;
        } elseif ($answer1['weight'] == 0 && $answer2['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.two.three');
            $note .= $temp;
        } elseif ($answer1['weight'] == 0 && $answer2['weight'] == 2) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.two.four');
            $note .= $temp;
        } elseif ($answer1['weight'] == 3 && $answer2['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.two.five');
            $note .= $temp;
        } elseif ($answer1['weight'] == 3 && $answer2['weight'] == 2) {
            $temp = ParamConfig::getValue('testing.figure.summary.part.two.six');
            $note .= $temp;
        }

        if ($answer8['weight'] > 0 && $answer9['weight'] == 0) {

            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.one.one');
            $note .= $temp;
        } elseif ($answer8['weight'] > 0 && $answer9['weight'] == 1) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.one.two');
            $note .= $temp;
        } elseif ($answer8['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.two.one');
            $note .= $temp;
        }


        if ($answer19['weight'] == 0.4) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.one');
            $note .= $temp;
        } elseif ($answer19['weight'] == 0.1) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.two');
            $note .= $temp;
        } elseif ($answer19['weight'] == 0.2) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.three');
            $note .= $temp;
        } elseif ($answer19['weight'] == 0.5) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.four');
            $note .= $temp;
        } elseif ($answer19['weight'] == 0.6) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.five');
            $note .= $temp;
        } elseif ($answer19['weight'] == 0.3) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.six');
            $note .= $temp;
        } elseif ($answer19['weight'] == 0.7) {
            $temp = ParamConfig::getValue('testing.figure.lifeAnalysisAndSuggestions.part.three.seven');
            $note .= $temp;
        }

        $obj = $this->summaryObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);

        LogHelper::log($obj);
        return $obj;
    }


    protected function getBodyStrengthParametersAttr()
    {
        $note = '';
        $extra = '';
        $answer22 = $this->getDefaultAnswerObjByIndex(22);
        $answer21 = $this->getDefaultAnswerObjByIndex(21);
        $answer1 = $this->getDefaultAnswerObjByIndex(1);
        $answer23 = $this->getDefaultAnswerObjByIndex(23);
        $answer25 = $this->getDefaultAnswerObjByIndex(25);
        $answer6 = $this->getDefaultAnswerObjByIndex(6);
        if ($answer22['weight'] == $answer1['weight']) {
            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.one.one');
            $note .= $temp;
        } elseif ($answer22['weight'] != $answer1['weight']) {
            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.one.two');
            $note .= $temp;
        }
        $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.two.two');
        $temp = str_replace('@21',round(doubleval($answer21['weight']),2), $temp);
        $note .= $temp;
        $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.two.one');
        $note .= $temp;
        if ($answer23['weight'] == 0) {

            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.three.one');
            $note .= $temp;
        } elseif ($answer23['weight'] > 0) {
            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.three.two');
            $note .= $temp;
            if ($answer23['weight'] == 1) {
                $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.three.three');
                $temp = str_replace('$6', $answer6['answer'], $temp);
                $note .= $temp;
            } elseif ($answer23['weight'] == 2) {
                $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.three.four');
                $temp = str_replace('$6', $answer6['answer'], $temp);
                $note .= $temp;
            }
        }

        if ($answer25['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.four.one');
            $note .= $temp;
        } elseif ($answer25['weight'] == 1) {
            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.four.two');
            $note .= $temp;
        } elseif ($answer25['weight'] == 2) {
            $temp = ParamConfig::getValue('testing.figure.bodyStrengthParameters.part.four.three');
            $note .= $temp;
        }

        $obj = $this->bodyStrengthParametersObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);
        LogHelper::log($obj);
        return $obj;
    }

    protected function getDietaryAnalysisAndRecommendationsAttr()
    {
        $note = '';
        $extra = '';
        $answer13 = $this->getDefaultAnswerObjByIndex(13);
        $answer14 = $this->getDefaultAnswerObjByIndex(14);
        $answer15 = $this->getDefaultAnswerObjByIndex(15);

        if ($answer13['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.one.one');
            $note .= $temp;
        } elseif ($answer13['weight'] != 0) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.one.two');
            $note .= $temp;
        }

        if ($answer14['weight'] == 2) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.two.one');
            $note .= $temp;
        } elseif ($answer14['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.two.two');
            $note .= $temp;
        } elseif ($answer14['weight'] == -2) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.two.three');
            $note .= $temp;
        }

        if ($answer15['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.three.one');
            $note .= $temp;
        } elseif ($answer15['weight'] == 2) {
            $temp = ParamConfig::getValue('testing.figure.dietaryAnalysisAndRecommendations.part.three.two');
            $note .= $temp;
        }

        $obj = $this->dietaryAnalysisAndRecommendationsObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);
        LogHelper::log($obj);
        return $obj;
    }

    protected function getPhysicalAnalysisAndSuggestionAttr()
    {
        $note = '';
        $extra = '';
        $answer16 = $this->getDefaultAnswerObjByIndex(16);
        $answer27 = $this->getDefaultAnswerObjByIndex(27);
        $answer17 = $this->getDefaultAnswerObjByIndex(17);
        $answer3 = $this->getDefaultAnswerObjByIndex(3);

        $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.one.one');
        $note .= $temp;
        if ($answer16['weight'] == 0) {

            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.two.one');
            $note .= $temp;
        } elseif ($answer16['weight'] > 0) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.two.two');
            $note .= $temp;
        }
        if ($answer27['weight'] < 0) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.three.one');
            $note .= $temp;
        } elseif ($answer27['weight'] >= 0) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.three.two');
            $note .= $temp;
        }
        if ($answer17['weight'] < 0) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.four.one');
            $note .= $temp;
        } elseif ($answer17['weight'] == 0) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.four.two');
            $note .= $temp;
        }
        if ($answer3['weight'] == 1) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.five.one');
            $note .= $temp;
        } elseif ($answer3['weight'] > 1) {
            $temp = ParamConfig::getValue('testing.figure.physicalAnalysisAndSuggestion.part.five.two');
            $note .= $temp;
        }

        $obj = $this->physicalAnalysisAndSuggestionObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);
        LogHelper::log($obj);
        return $obj;
    }



    /**
     * 图标对应的BMI数据
     */
    protected function getChatparam()
    {
        $data = [];
       /* $obj['key'] = 'BMI';
        $answer21 = $this->getDefaultAnswerObjByIndex(21);
        $obj['value'] = round(doubleval($answer21['weight']), 2);
        array_push($data, $obj);*/
        return $data;
    }


    protected function compactAttrObj($id, $title, $note, $extra = '')
    {
        return [
            'attr_id' => $id,
            'title' => $title,
            'note' => $note,
            'extra' => $extra
        ];
    }

    protected function getDefaultAnswerObjByIndex($index)
    {
        $obj = [];
        foreach ($this->answers as $answer) {
            if ($index == $answer['index']) {
                $obj = $answer;
                break;
            }
        }
        return $obj;
    }

    /**将answer转换为数组
     * @param $answerStr
     * @return array
     */
    protected function convertAnswer($answerStr)
    {
        LogHelper::log('FINAL ANSWER STR============>');
        LogHelper::log($answerStr);
        $answers = [];
        $answerArr = explode('##', $answerStr);
        if (count($answerArr) > 0) {
            foreach ($answerArr as $answer) {
                $singleArr = explode(':', $answer);
                if (count($singleArr) > 0) {
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers, $obj);
                }
            }
        }
        return $answers;
    }

}